use strict;
use Test::More tests => 1;
use Test::Deep;

BEGIN {
use_ok( 'Parse::BitTorrentConsole' );
}

diag( "Testing Parse::BitTorrentConsole $Parse::BitTorrentConsole::VERSION" );

# some infrastructure to make testing easier
my $expected_counter = 0;
my $script_counter = 0;
my @pending_events;
my $script = '';

sub events {
  my ($event, $args, @extra);
  
  if (!@pending_events) {
    fail("Received event '$event' for line '$args->{line}': unexpected");
    return;
  }
  
  my $expected = shift @pending_events;
  
  is(++$expected_counter, $expected->{count}, 'the event was received in the proper order');
  is($event, $expected->{event}, 'got the proper event');
  cmp_deeply($args, $expected->{args}, 'got expected arguments') if $expected->{args};
  
  return 'ok';
}

sub script {
  my ($line, $event) = @_;
  
  $script .= $line . "\n";
  return unless $event;
  
  if (exists $event->{args}) {
    $event->{args}{line} = $line;
  }
  
  $event->{count} = ++$script_counter;
  
  push @pending_events, $event;
}

# now the tests

my $filename = 'my_filename_is_here';
my $destination = 'my_destination_here';

script("saving:         $filename",             { event => 'filename', args => { filename => $filename } });
script("file size:      760,499,847 (725 MiB)", { event => 'filesize', args => { filesize => 760499847 } });
script("percent done:   0.0");
script("time left:      Initial startup");
script("download to:    $destination", { event => 'destination', args => { destination => $destination } });
script("download rate:  ---");
script("upload rate:    ---");
script("share rating:   ");
script("seed status:    ");
script("peer status:    ");
script("");

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.0");
script("time left:      downloading");
script("download to:    $destination");
script("download rate:  0.0 KB/s");
script("upload rate:    0.0 KB/s");
script("share rating:   oo  (0.0 MB up / 0.0 MB down)");
script("seed status:    1 seen now, plus 0 distributed copies (1:0.0%)");
script("peer status:    0 seen now");
script("", { event => 'status', args => {
    percent   => '0.0',
    status    => 'downloading',
    rate_down => { value => '0.0', unit => 'KB/s' },
    rate_up   => { value => '0.0', unit => 'KB/s' },
    share     => { up => { value => '0.0', unit => 'MB' }, down => { value => '0.0', unit => 'MB' }, rating => '' },
    peers     => { seeds => 1, distributed => 0, other => 0 },
}});

script("");

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.0");
script("time left:      downloading");
script("download to:    $destination");
script("download rate:  0.0 KB/s");
script("upload rate:    0.0 KB/s");
script("share rating:   oo  (0.0 MB up / 0.0 MB down)");
script("seed status:    1 seen now, plus 0 distributed copies (1:0.0%)");
script("peer status:    0 seen now");
script("");

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.0");
script("time left:      downloading");
script("download to:    $destination");
script("download rate:  0.0 KB/s");
script("upload rate:    0.0 KB/s");
script("share rating:   oo  (0.0 MB up / 0.0 MB down)");
script("seed status:    1 seen now, plus 0 distributed copies (1:0.0%)");
script("peer status:    1 seen now");
script("", { event => 'status', args => {
    percent   => '0.0',
    status    => 'downloading',
    rate_down => { value => '0.0', unit => 'KB/s' },
    rate_up   => { value => '0.0', unit => 'KB/s' },
    share     => { up => { value => '0.0', unit => 'MB' }, down => { value => '0.0', unit => 'MB' }, rating => '' },
    peers     => { seeds => 1, distributed => 0, other => 1 },
}});

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.0");
script("time left:      downloading");
script("download to:    $destination");
script("download rate:  0.0 KB/s");
script("upload rate:    0.0 KB/s");
script("share rating:   oo  (0.0 MB up / 0.0 MB down)");
script("seed status:    2 seen now, plus 0 distributed copies (1:0.0%)");
script("peer status:    0 seen now");
script("", { event => 'status', args => {
    percent   => '0.0',
    status    => 'downloading',
    rate_down => { value => '0.0', unit => 'KB/s' },
    rate_up   => { value => '0.0', unit => 'KB/s' },
    share     => { up => { value => '0.0', unit => 'MB' }, down => { value => '0.0', unit => 'MB' }, rating => '' },
    peers     => { seeds => 2, distributed => 0, other => 0 },
}});

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.0");
script("time left:      downloading");
script("download to:    $destination");
script("download rate:  0.8 KB/s");
script("upload rate:    0.0 KB/s");
script("share rating:   0.000  (0.0 MB up / 0.0 MB down)");
script("seed status:    2 seen now, plus 0 distributed copies (1:0.0%)");
script("peer status:    0 seen now");
script("", { event => 'status', args => {
    percent   => '0.0',
    status    => 'downloading',
    rate_down => { value => '0.8', unit => 'KB/s' },
    rate_up   => { value => '0.0', unit => 'KB/s' },
    share     => { up => { value => '0.0', unit => 'MB' }, down => { value => '0.0', unit => 'MB' }, rating => '0.000' },
    peers     => { seeds => 2, distributed => 0, other => 0 },
}});


script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.0");
script("time left:      finishing in 185:44:21");
script("download to:    $destination");
script("download rate:  1.2 KB/s");
script("upload rate:    0.0 KB/s");
script("share rating:   0.000  (0.0 MB up / 0.0 MB down)");
script("seed status:    2 seen now, plus 0 distributed copies (1:0.0%)");
script("peer status:    0 seen now");
script("", { event => 'status', args => {
    percent   => '0.0',
    time_left => '185:44:21',
    status    => 'downloading',
    rate_down => { value => '1.2', unit => 'KB/s' },
    rate_up   => { value => '0.0', unit => 'KB/s' },
    share     => { up => { value => '0.0', unit => 'MB' }, down => { value => '0.0', unit => 'MB' }, rating => '0.000' },
    peers     => { seeds => 2, distributed => 0, other => 0 },
}});

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.0");
script("time left:      finishing in 97:06:22");
script("download to:    $destination");
script("download rate:  2.3 KB/s");
script("upload rate:    0.0 KB/s");
script("share rating:   0.000  (0.0 MB up / 0.1 MB down)");
script("seed status:    2 seen now, plus 0 distributed copies (1:0.0%)");
script("peer status:    0 seen now");
script("", { event => 'status', args => {
    percent   => '0.0',
    time_left => '97:06:22',
    status    => 'downloading',
    rate_down => { value => '2.3', unit => 'KB/s' },
    rate_up   => { value => '0.0', unit => 'KB/s' },
    share     => { up => { value => '0.0', unit => 'MB' }, down => { value => '0.1', unit => 'MB' }, rating => '0.000' },
    peers     => { seeds => 2, distributed => 0, other => 0 },
}});

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   0.1");
script("time left:      finishing in 57:19:44");
script("download to:    $destination");
script("download rate:  4.9 KB/s");
script("upload rate:    0.7 KB/s");
script("share rating:   0.013  (0.0 MB up / 1.0 MB down)");
script("seed status:    3 seen now, plus 0 distributed copies (1:97.0%, 2:22.8%, 3:0.0%)");
script("peer status:    2 seen now");
script("", { event => 'status', args => {
    percent   => '0.1',
    time_left => '57:19:44',
    status    => 'downloading',
    rate_down => { value => '4.9', unit => 'KB/s' },
    rate_up   => { value => '0.7', unit => 'KB/s' },
    share     => { up => { value => '0.0', unit => 'MB' }, down => { value => '1.0', unit => 'MB' }, rating => '0.013' },
    peers     => { seeds => 3, distributed => 0, other => 2 },
}});

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   67.1");
script("time left:      finishing in 3:04:38");
script("download to:    $destination");
script("download rate:  14.5 KB/s");
script("upload rate:    18.5 KB/s");
script("share rating:   0.344  (167.7 MB up / 487.0 MB down)");
script("seed status:    5 seen now, plus 1 distributed copies (2:93.2%, 3:38.6%, 4:6.8%)");
script("peer status:    6 seen now");
script("", { event => 'status', args => {
    percent   => '67.1',
    time_left => '3:04:38',
    status    => 'downloading',
    rate_down => { value => '14.5', unit => 'KB/s' },
    rate_up   => { value => '18.5', unit => 'KB/s' },
    share     => { up => { value => '167.7', unit => 'MB' }, down => { value => '487.0', unit => 'MB' }, rating => '0.344' },
    peers     => { seeds => 5, distributed => 1, other => 6 },
}});

script("saving:         $filename");
script("file size:      760,499,847 (725 MiB)");
script("percent done:   67.2");
script("time left:      finishing in 3:04:25");
script("download to:    $destination");
script("download rate:  11.2 KB/s");
script("upload rate:    13.0 KB/s");
script("share rating:   0.346  (168.9 MB up / 488.0 MB down)");
script("seed status:    5 seen now, plus 1 distributed copies (2:93.4%, 3:38.8%, 4:6.8%)");
script("peer status:    6 seen now");
script("", { event => 'status', args => {
    percent   => '67.2',
    time_left => '3:04:25',
    status    => 'downloading',
    rate_down => { value => '11.2', unit => 'KB/s' },
    rate_up   => { value => '13.0', unit => 'KB/s' },
    share     => { up => { value => '168.9', unit => 'MB' }, down => { value => '488.0', unit => 'MB' }, rating => '0.346' },
    peers     => { seeds => 5, distributed => 1, other => 6 },
}});

my $bt_parser = Parse::BitTorrentConsole->new({callback => \&events});
ok($bt_parser, 'created parser sucessfully');

$bt_parser->parse_more($script);
